package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyJinFuJuJian;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**2
 *  安心分期-成都二选五选api
 *
 *  @author 吴佳宝
 */
public abstract class ApiOrgPhoneMd5JinFuJuJian implements ApiOrgPhoneMd5ConvertHandler {

    public static final Integer FILE_ID = 45;
    public static final String PROMOTION = "4504";

    @Override
    public Object dataExchange(ApiDataExchangeParameter ApiDataChangeMessage) throws Exception {
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobile", ApiDataChangeMessage.getPhoneMd5());
        });
        return map;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyMessage) throws Exception {
        OrgApplyJinFuJuJian data = new OrgApplyJinFuJuJian();
        data.setFile_id(FILE_ID);
        data.setPromotion(PROMOTION);
        // 手机号明文
        data.setMobile(apiApplyMessage.getPhone());
        // 用户名
        data.setName(apiApplyMessage.getUserName());
        // 城市
        data.setCity(apiApplyMessage.getCity());
        // 年龄
        data.setAge(Integer.valueOf(apiApplyMessage.getAge()));
        // 性别
        Integer sex = ChooseEq.create(apiApplyMessage.getSex())
                .when("0", 0)
                .when("1", 1)
                .end(1);
        data.setSex(sex);
        // 房产信息
        Integer realEstateInfo = ChooseEq.create(apiApplyMessage.getRealEstateInfo())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .when("5", 1)
                .when("6", 1)
                .end(0);
        data.setIs_house(realEstateInfo);
        // 车产信息
        Integer carInfo = ChooseEq.create(apiApplyMessage.getCarInfo())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .end(0);
        data.setIs_car(carInfo);
        // 营业执照
        Integer bizLicenseYear = ChooseEq.create(apiApplyMessage.getBizLicenseYear())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .end(0);
        data.setIs_company(bizLicenseYear);
        // 社保
        Integer socialSecurity = ChooseEq.create(apiApplyMessage.getSocialSecurity())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .end(0);
        data.setIs_social(socialSecurity);
        // 公积金
        Integer providentFund = ChooseEq.create(apiApplyMessage.getProvidentFund())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .end(0);
        data.setIs_fund(providentFund);
        // 保险保单
        Integer insurancePolicy = ChooseEq.create(apiApplyMessage.getInsurancePolicy())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .end(0);
        data.setIs_insurance(insurancePolicy);
        // 信用卡（0-无；1-2万元以内；2-2万元以上
        Integer creditLimit = ChooseEq.create(apiApplyMessage.getCreditLimit())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .when("5", 1)
                .when("6", 1)
                .end(0);
        data.setIs_credit(creditLimit);
        // 贷款金额(元)
        String loanAmount = ChooseEq.create(apiApplyMessage.getLoanAmount())
                .when("1", "30000")
                .when("2", "50000")
                .when("3", "100000")
                .when("4", "200000")
                .end();
        data.setMoney_demand(loanAmount);
        // 工资发放形式
        Integer salaryPayMethod = ChooseEq.create(apiApplyMessage.getSalaryPayMethod())
                .when("1", 1)
                .when("2", 0)
                .when("3", 0)
                .end(0);
        data.setIs_work(salaryPayMethod);
        data.setIs_tax(0);
        data.setWebank(0);
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
